package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.CacheNamespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.Cacheable;

import java.util.List;
import java.util.Set;

/**
 * ClassName: SetMealDIshController
 * Package: com.sky.controller.admin
 * Description:
 *
 * @Author 龙培
 * @Create 2025/3/3 13:44
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐菜品管理")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealService setmealService1;

    /**
     * 保存套餐菜品信息
     * @param setmealDTO
     * @return
     */
    @ApiOperation(value = "保存套餐菜品信息")
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.id")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        log.info("保存套餐菜品信息：{}", setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    /**
     * 分页查询套餐菜品信息
     */
    @ApiOperation(value = "分页查询套餐菜品信息")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("分页查询套餐菜品信息：{}", setmealPageQueryDTO);
        PageResult pageResult  = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除套餐菜品信息
     */
    @ApiOperation(value = "批量删除套餐菜品信息")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result delete(@RequestParam List<Long> ids) {
        log.info("删除套餐菜品信息：{}", ids);
        setmealService.delete(ids);
        return Result.success();
    }

    /**
     * 根据id获取套餐菜品信息
     */
    @ApiOperation(value = "根据id获取套餐菜品信息")
    @GetMapping("/{id}")
    public Result<SetmealVO> get(@PathVariable Long id) {
        log.info("根据id获取套餐菜品信息：{}", id);
        SetmealVO setmealVO =setmealService1.getSetmealWithDishById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐菜品信息
     */
    @ApiOperation(value = "修改套餐菜品信息")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐菜品信息：{}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐启用/禁用
     */
    @ApiOperation(value = "套餐启用/禁用")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result updateStatus(@PathVariable Integer status, Long id) {
        log.info("套餐启用/禁用：{}", status);
        setmealService.updateStatus(id, status);
        return Result.success();
    }
}
